from scrapy.pipelines.images import ImagesPipeline
from scrapy.http import Request
from items.ugitems import SongItem

"""
专辑图片下载
"""


class AlbumImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        if(item and isinstance(item, SongItem)):
        # if(item and item.contains("album_cover")):
            url = item["album_cover"]
            if url and url != None and url != "":
                yield Request(url, meta=item)

    def file_path(self, request, response=None, info=None, item=None):
        # title = request.meta['title'].replace('/', '_') #防止路径问题
        title = (
            request.meta["song_name"] + "_" + str(request.meta["id"]) + "_" + "album"
        )
        return f"./album/{title}.jpg"


"""
艺术家图片下载
"""


class ArtistImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        if(item and isinstance(item,SongItem)):
        # if(item and item.contains("artist_cover")):
            url = item["artist_cover"]
            if url and url != None and url != "":
                yield Request(url, meta=item)

    def file_path(self, request, response=None, info=None, item=None):
        # title = request.meta['title'].replace('/', '_') #防止路径问题
        title = (
            request.meta["artist_name"]
            + "_"
            + str(request.meta["id"])
            + "_"
            + "artist_cover"
        )
        return f"./artist/{title}.jpg"
